import { getOSSToken } from '@/common/api/hbMarket';
import { ref } from 'vue';

export const useOssToken = function () {
  const client = ref<any>(null);
  const getOssToken = async () => {
    try {
      const res = await getOSSToken({});
      const token = res.result;
      const OSS = await import('ali-oss');
      client.value = new OSS.default({
        // region 字段必须以oss-开头
        region: 'oss-cn-qingdao',
        accessKeyId: token.AccessKeyId,
        accessKeySecret: token.AccessKeySecret,
        stsToken: token.SecurityToken,
        // 填写Bucket名称。
        bucket: token.bucketName,
        // 刷新临时访问凭证。
        refreshSTSToken: async () => {
          const refreshToken = await getOSSToken({});
          const tokenInfo = refreshToken.result;

          return {
            accessKeyId: tokenInfo.AccessKeyId,
            accessKeySecret: tokenInfo.AccessKeySecret,
            stsToken: tokenInfo.SecurityToken,
          };
        },
      });
    } catch (err) {
      console.log('err: ', err);
    }
  };
  getOssToken();

  return { client };
};
